package com.auhaix.controller;

import com.auhaix.common.Response;
import com.auhaix.common.ResponseUtils;
import com.auhaix.entity.User;
import com.auhaix.common.ResponseEnum;
import com.auhaix.repository.UserRepository;
import com.auhaix.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * Author: Auhaix
 * E-mail: xh@qiandw.com
 * Description:
 * CreatedTime: 2017/7/26 13:26
 * Modified by:
 */
@RestController
@RequestMapping(value = "/users")
public class UserController {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private UserService userService;

    /**
     * 查询所有列表数据
     *
     * @return List<User>
     */
    @GetMapping(value = "/")
    public List<User> getList() {
        return userRepository.findAll();
    }

    /**
     * 添加
     *
     * @param user
     * @param bindingResult
     * @return
     */
    @PostMapping(value = "/add")
    public Response userAdd(@Valid User user,BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseUtils.error("990001", bindingResult.getFieldError().getDefaultMessage());
        }
        return ResponseUtils.success(ResponseEnum.SUCCESS, userRepository.save(user));
    }

    /**
     * 查询一个user
     *
     * @param id id
     * @return User
     */
    @GetMapping(value = "/{id}")
    public User getOne(@PathVariable("id") Integer id) {
        return userRepository.findOne(id);
    }

    /**
     * 更新一个user
     *
     * @param user 对象
     * @return User
     */
    @PutMapping(value = "/edit")
    public User girlEdit(User user) {
        return userRepository.save(user);
    }

    /**
     * 删除一个user
     *
     * @param id id
     */
    @DeleteMapping(value = "/{id}")
    public void delOne(@PathVariable("id") Integer id) {
        userRepository.delete(id);
    }

    /**
     * 根据年龄查询
     *
     * @param age
     * @return
     */
    @GetMapping(value = "/age/{age}")
    public List<User> findListByAge(@PathVariable("age") Integer age) {
        return userRepository.findByAge(age);
    }

    /**
     * 事务
     */
    @PostMapping(value = "/addtwo")
    public void insertTwoUser() {

        try {
            userService.insertTwo();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 返回对象字符串
     *
     * @param id
     * @return
     */
    @PostMapping(value = "/{id}")
    public String userToString(@PathVariable("id") Integer id) {
        User user = userRepository.findOne(id);
        if (user != null) {
            return user.toString();
        } else {
            return "未找到对象";
        }
    }

    /**
     * 升职加薪在望
     *
     * @param id
     */
    @PostMapping(value = "/promotion")
    public void promotion(@RequestParam("id") Integer id, @RequestParam("duty") String duty) {
        userService.promotion(id, duty);
    }
}
